package com.py.korona.adapters;

import java.io.Serializable;
import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;

public class CustomListAdapterWrapper implements LoaderCallbacks<Cursor>, Serializable {
    private static final long        serialVersionUID = 1L;
    private Activity                 context;
    private SimpleCursorAdapter      listAdapter_;
    /**
     * 
     */
    private CustomListAdapterMapping mapping;

    public CustomListAdapterWrapper() {
    }

    public CustomListAdapterWrapper(final Activity context) {
        setContext(context);
    }

    public final ListAdapter adapter() {
        return listAdapter_;
    }

    public Activity getContext() {
        return this.context;
    }

    public CustomListAdapterMapping getMapping() {
        return mapping;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        if (null == this.context) {
            this.listAdapter_ = null;
        } else {
            this.listAdapter_ = new SimpleCursorAdapter(context, mapping.bindToLayout(), null, mapping.bindFromColumns(), mapping.bindToLayoutViews(), 0);
            this.listAdapter_.setViewBinder(mapping.binder());
        }
        return new CursorLoader(context, mapping.contentURI(), mapping.contentProperties(), mapping.filter(), mapping.filterArgs(), null);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        if (null != listAdapter_)
            listAdapter_.swapCursor(null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        if (null != listAdapter_)
            listAdapter_.swapCursor(arg1);
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public void setMapping(CustomListAdapterMapping mapping) {
        this.mapping = mapping;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
}
